package com.conf.swagger;

import com.github.xiaoymin.swaggerbootstrapui.annotations.EnableSwaggerBootstrapUI;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.ApiKey;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

/**
 * 文档：http://springfox.github.io/springfox/docs/current/
 *
 * @author zhangweiwei
 * @date 2018-11-01
 */
@EnableSwagger2
@EnableSwaggerBootstrapUI
@Configuration
public class SwaggerConfig {
    
    @Value("${htwz.swagger.title:'bbs-server'}")
    private String title;
    
    @Autowired
    private Environment environment;
    
    
//    @Bean
//    public Docket createAppletApi() {
//        return getDocketByGroup("前端展示", AuthRole.DISPLAY);
//    }
//
//
//    @Bean
//    public Docket createRegoinApi() {
//        return getDocketByGroup("管理端", AuthRole.ADMIN);
//    }
//
    /**
     * 获取swagger分组
     *
     * @param groupName
     * @param roleUrl
     * @return
     */
    private Docket getDocketByGroup(String groupName, String roleUrl) {
        List<ApiKey> apiKeyList = new ArrayList<>();
        apiKeyList.add(new ApiKey("token", "token", "header"));
        List<Parameter> parameterList = new ArrayList<>();
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .groupName(groupName)
                .select()
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .paths(PathSelectors.regex(".*/" + roleUrl + "/.*"))
                .build()
                .globalOperationParameters(parameterList).securitySchemes(apiKeyList);
        
        
    }
    
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title(title + ":" + environment.getProperty("spring.application.name"))
                .description("ENV: " + environment.getProperty("spring.profiles.active"))
                .contact(new Contact("亨通网智", "git.schtwz.cn", "mail.htgd.com.cn"))
                .version("1.0.0")
                .build();
    }
}
